package net.hn.hnms.biz.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.process.domain.WorkTicketPo;
import net.hn.hnms.biz.process.mapper.WorkTicketMapper;
import net.hn.hnms.biz.process.service.WorkTicketService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service()
public class WorkTicketServiceImpl implements WorkTicketService {
    @Resource
    private WorkTicketMapper workTicketMapper;

    /**
     * 功能描述: 分页条件查询工作票列表
     *
     * @param pageQuery
     * @param workTicketPo
     * @return net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo<net.hn.hnms.biz.process.domain.WorkTicketPo>
     * @auther: WangLong
     * @date: 2022/8/5 16:00
     */

    @Override
    public TableDataInfo<WorkTicketPo> ticketPageList(PageQuery pageQuery, WorkTicketPo workTicketPo) {
        Page<WorkTicketPo> workTicketPoPage = workTicketMapper.selectPage(pageQuery.build(), new LambdaQueryWrapper<WorkTicketPo>()
                .eq(StringUtils.isNotBlank(workTicketPo.getTicketTypeId()), WorkTicketPo::getTicketTypeId, workTicketPo.getTicketTypeId())
                .eq(StringUtils.isNotBlank(workTicketPo.getTicketTypeName()), WorkTicketPo::getTicketTypeName, workTicketPo.getTicketTypeName())
                .like(StringUtils.isNotBlank(workTicketPo.getTicketName()), WorkTicketPo::getTicketName, workTicketPo.getTicketName())
                .eq(StringUtils.isNotBlank(workTicketPo.getTicketStatus()), WorkTicketPo::getTicketStatus, workTicketPo.getTicketStatus())
                //煤矿编码
                .eq(StringUtils.isNotBlank(workTicketPo.getMineCode()), WorkTicketPo::getMineCode, workTicketPo.getMineCode()));


        if (workTicketPoPage.getRecords().isEmpty()) {
             workTicketPoPage.setRecords(new ArrayList<>());
        }
        return TableDataInfo.build(workTicketPoPage);
    }

    /**
     * 功能描述: 添加工作票
     *
     * @param workTicketPo
     * @return boolean
     * @auther: WangLong
     * @date: 2022/8/5 16:08
     */

    @Override
    public boolean ticketAdd(WorkTicketPo workTicketPo) {
        //先查工作票分类是否已经存在
        String ticketTypeId = workTicketPo.getTicketTypeId();
        List<WorkTicketPo> workTicketPos = workTicketMapper.selectList(new LambdaQueryWrapper<WorkTicketPo>().eq(WorkTicketPo::getTicketTypeId, ticketTypeId));
        if (!workTicketPos.isEmpty()) {
            throw new HuaNengException(Constants.FAIL, "该工作票分类已经存在，请删除后再次添加");
        }

        //流程状态
        workTicketPo.setFlowStatus("0");
        //步骤状态
        workTicketPo.setStepState("0");
        int insert = workTicketMapper.insert(workTicketPo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 工作票修改
     * @auther: WangLong
     * @date: 2022/8/5 16:10
     * @param workTicketPo
     * @return boolean
     */

    @Override
    public boolean updateById(WorkTicketPo workTicketPo) {
        //获取工作票ID
        String ticketId = workTicketPo.getTicketId();
        WorkTicketPo workTicketPo1 = workTicketMapper.selectById(ticketId);
        if (workTicketPo1 == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该工作票ID" + ticketId);
        }

        //获取请求分类ID
        String ticketTypeId = workTicketPo.getTicketTypeId();
        //获取数据库分类ID
        String ticketTypeId1 = workTicketPo1.getTicketTypeId();
        //判断分类ID是否相同
        if (!ticketTypeId1.equals(ticketTypeId)) {
            //不相同，查询数据库的请求分类ID是否已经存在
            List<WorkTicketPo> workTicketPos = workTicketMapper.selectList(new LambdaQueryWrapper<WorkTicketPo>().eq(WorkTicketPo::getTicketTypeId, ticketTypeId));
            if (!workTicketPos.isEmpty()) {
                //分类ID已经存在，不允许修改
                throw new HuaNengException(Constants.FAIL, "此分类已存在，请修改为其他分类");
            }
        }

        int i = workTicketMapper.updateById(workTicketPo);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 工作票批量删除
     * @auther: WangLong
     * @date: 2022/8/5 16:12
     * @param idList
     * @return boolean
     */

    @Override
    public boolean removeByIds(List<String> idList) {
        int i = workTicketMapper.deleteBatchIds(idList);
        if (i == idList.size()) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 工作票状态修改
     * @auther: WangLong
     * @date: 2022/8/5 16:16
     * @param id
     * @return java.lang.Boolean
     */

    @Override
    public Boolean amendStatusById(String id) {
        WorkTicketPo workTicketPo = workTicketMapper.selectById(id);
        if (workTicketPo == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该工作票");
        }
        String ticketStatus = workTicketPo.getTicketStatus();
        if ("0".equals(ticketStatus)) {
            workTicketPo.setTicketStatus("1");
        } else if ("1".equals(ticketStatus)) {
            workTicketPo.setTicketStatus("0");
        } else {
            throw new HuaNengException(Constants.FAIL,"工作票状态错误,请联系管理员");
        }
        int i = workTicketMapper.updateById(workTicketPo);
        if (i > 0) {
            return true;
        }
        return false;
    }

}
